package ch.trainingtest.business;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import ch.trainingtest.dao.TaskDao;
import ch.trainingtest.dao.TaskHistoryDao;
import ch.trainingtest.dto.TaskHistoryDto;
import ch.trainingtest.dto.UserDto;
import ch.trainingtest.entity.Task;
import ch.trainingtest.entity.TaskHistory;

@Stateless
public class TaskHistoryManagementBean {

	@EJB
	private TaskHistoryDao taskHistoryDao;
	
	@EJB
	private TaskDao tasKDao;
	
	public List<TaskHistoryDto> getAllHistoryOfTask(int taskID) {
		Task task = tasKDao.find(taskID);
		if(task != null) {
			List<TaskHistoryDto > result  = new ArrayList<TaskHistoryDto>();
			List<TaskHistory> listTaskHistory = task.getListTaskHistory();
			for(TaskHistory taskHistory: listTaskHistory) {
				TaskHistoryDto dto = new TaskHistoryDto();
				dto.setId(taskHistory.getId());
				dto.setAction(taskHistory.getAction());
				SimpleDateFormat datetimeFormat = 
					      new SimpleDateFormat ("dd.MM.yyyy hh a");
				dto.setCreatedTime(datetimeFormat.format(taskHistory.getCreatedTime()));
				dto.setModifiedBy(taskHistory.getModifiedBy());
				dto.setFrom(taskHistory.getFrom());
				dto.setTo(taskHistory.getTo());
				result.add(dto);
			}
			return result;
		}
		return null;
	}
	
}
